package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.domain.ResponseResult;
import com.wzy.domain.entity.Article;
import com.wzy.domain.entity.Collection;
import com.wzy.domain.vo.ArticleListVO;
import com.wzy.exception.SystemException;
import com.wzy.mapper.ArticleMapper;
import com.wzy.mapper.CollectionMapper;
import com.wzy.service.CollectionService;
import com.wzy.service.UserCommonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.wzy.constant.SystemConstants.*;
import static com.wzy.enums.AppHttpCodeEnum.PARAMETER_ERROR;

/**
 * 收藏业务层
 */
@Service
@RequiredArgsConstructor
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection>
        implements CollectionService
{
    private final HttpServletRequest httpServletRequest;
    private final ArticleMapper articleMapper;
    private final CollectionMapper collectionMapper;
    private final UserCommonService userCommonService;

    /**
     * 文章是否收藏
     */
    @Override
    public ResponseResult<Boolean> isCollected(Long articleId)
    {
        Long userId = userCommonService.getRedisUserId(httpServletRequest);
        Collection collection = collectionMapper.selectOne(new LambdaQueryWrapper<Collection>()
                .eq(Collection::getArticleId, articleId)
                .eq(Collection::getUserId, userId));
        boolean result = true;
        if (collection == null)
        {
            result = false;
        }
        return ResponseResult.okResult(result);
    }

    /**
     * 收藏/取消收藏文章
     */
    @Override
    public ResponseResult<Void> addOrCancelCollect(Long articleId, String flag)
    {
        Long userId = userCommonService.getRedisUserId(httpServletRequest);

        if (flag.equals(IS_TRUE))
        {
            // 校验文章是否存在
            LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Article::getArticleStatus, RELEASE);
            queryWrapper.eq(Article::getId, articleId);
            if (articleMapper.selectOne(queryWrapper) == null)
            {
                throw new SystemException(PARAMETER_ERROR, "文章不存在");
            }
            LambdaQueryWrapper<Collection> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Collection::getArticleId, articleId);
            queryWrapper1.eq(Collection::getUserId, userId);
            if (collectionMapper.selectOne(queryWrapper1) != null)
            {
                throw new SystemException(PARAMETER_ERROR, "该文章已收藏");
            }
            // 添加收藏
            Collection collection = new Collection();
            collection.setArticleId(articleId);
            collection.setUserId(userId);
            collectionMapper.insert(collection);
        }
        else if (flag.equals(IS_FALSE))
        {
            // 取消收藏文章
            LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Collection::getUserId, userId);
            queryWrapper.eq(Collection::getArticleId, articleId);
            collectionMapper.delete(queryWrapper);
        }

        return ResponseResult.okResult();
    }

    /**
     * 获取收藏文章列表
     */
    @Override
    public ResponseResult<List<ArticleListVO>> getCollectList()
    {
        Long userId = userCommonService.getRedisUserId(httpServletRequest);
        List<ArticleListVO> collectList = collectionMapper.getCollectList(userId);

        return ResponseResult.okResult(collectList);
    }

}




